package com.fr.chart.core.glyph;

import com.fr.base.FRFont;
import com.fr.base.StringUtils;
import com.fr.base.Style;
import com.fr.base.core.BaseCoreConstants;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.GlyphCondition;
import com.fr.chart.axis.SeriesAttrAlpha;
import com.fr.chart.axis.TextAttr;
import com.fr.chart.base.equals.Equals;
import com.fr.chart.core.RectangleGlyph;
import com.fr.chart.plot.SeriesCollection;
import com.fr.report.core.PaintUtils;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class TextGlyph extends RectangleGlyph
  implements GlyphCondition
{
  private static final long serialVersionUID = -3597929707303851528L;
  public static final String XML_TAG = "TextGlyph";
  private String text;
  private TextAttr textAttr;

  public TextGlyph()
  {
  }

  public TextGlyph(String paramString, TextAttr paramTextAttr)
  {
    this.text = paramString;
    this.textAttr = paramTextAttr;
  }

  public void setText(String paramString)
  {
    this.text = paramString;
  }

  public String getText()
  {
    return this.text;
  }

  public TextAttr getTextAttr()
  {
    return this.textAttr;
  }

  public void setTextAttr(TextAttr paramTextAttr)
  {
    this.textAttr = paramTextAttr;
  }

  public Iterator selectableChildren()
  {
    return Collections.EMPTY_LIST.iterator();
  }

  public void draw(Graphics paramGraphics)
  {
    super.draw(paramGraphics);
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Paint localPaint = localGraphics2D.getPaint();
    Composite localComposite = localGraphics2D.getComposite();
    localGraphics2D.setComposite(AlphaComposite.getInstance(3, 1.0F));
    if ((getText() != null) && (getBounds() != null))
      drawLabel(localGraphics2D, getText(), getTextAttr(), getBounds());
    localGraphics2D.setPaint(localPaint);
    localGraphics2D.setComposite(localComposite);
  }

  public static void drawLabel(Graphics paramGraphics, String paramString, TextAttr paramTextAttr, Rectangle2D paramRectangle2D)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    if (StringUtils.isEmpty(paramString))
      return;
    if (paramTextAttr == null)
      paramTextAttr = new TextAttr();
    localGraphics2D.translate(paramRectangle2D.getX(), paramRectangle2D.getY());
    if (paramTextAttr.getFRFont() != null)
      localStyle = Style.getInstance(paramTextAttr.getFRFont());
    else
      localStyle = Style.getInstance();
    Style localStyle = localStyle.deriveTextDirection(paramTextAttr.getDirection());
    localStyle = localStyle.deriveRotation(paramTextAttr.getRotation());
    localStyle = localStyle.deriveHorizontalAlignment(paramTextAttr.getHorizontalAlignment());
    localStyle = localStyle.deriveVerticalAlignment(paramTextAttr.getVerticalAlignment());
    localStyle = localStyle.deriveVerticalText(paramTextAttr.getAlignText());
    PaintUtils.paintContent(localGraphics2D, paramString, localStyle, (int)paramRectangle2D.getWidth() + 4, (int)paramRectangle2D.getHeight());
    localGraphics2D.translate(-paramRectangle2D.getX(), -paramRectangle2D.getY());
  }

  public Dimension2D preferredDimension()
  {
    return calculateTextDimension(this.text, this.textAttr);
  }

  public static Dimension2D calculateTextDimension(String paramString, TextAttr paramTextAttr)
  {
    if (StringUtils.isEmpty(paramString))
      return new Dimension(0, 0);
    FRFont localFRFont = (paramTextAttr == null) ? null : paramTextAttr.getFRFont();
    if (localFRFont == null)
      localFRFont = FRFont.getInstance();
    if (paramTextAttr == null)
      paramTextAttr = new TextAttr();
    Dimension2D localDimension2D = GraphHelper.stringDimension(paramString, localFRFont, paramTextAttr.getRotation(), BaseCoreConstants.DEFAULT_FRC);
    localDimension2D.setSize(localDimension2D.getWidth() + 2.0D, localDimension2D.getHeight() + 2.0D);
    if (paramTextAttr.getAlignText() == 1)
      localDimension2D.setSize(localDimension2D.getHeight(), localDimension2D.getHeight() * (paramString.length() + 0.2D));
    return localDimension2D;
  }

  public void dealCondition(SeriesCollection paramSeriesCollection, int paramInt)
  {
    SeriesAttrAlpha localSeriesAttrAlpha = (SeriesAttrAlpha)paramSeriesCollection.getSeriesCondition(new SeriesAttrAlpha(), paramInt);
    setAlpha(localSeriesAttrAlpha.getAlpha());
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof TextGlyph))
      return false;
    TextGlyph localTextGlyph = (TextGlyph)paramObject;
    if (!(super.equals(localTextGlyph)))
      return false;
    if (!(Equals.equals(localTextGlyph.getText(), getText())))
      return false;
    return (Equals.equals(localTextGlyph.getTextAttr(), getTextAttr()));
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Text"))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("text")) != null)
          setText(str2);
      }
      if (str1.equals("TextAttr"))
        this.textAttr = ((TextAttr)paramXMLableReader.readXMLObject(new TextAttr()));
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("TextGlyph");
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("Text").attr("text", getText()).end();
    if (getTextAttr() != null)
      this.textAttr.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    localJSONObject.put("text", this.text);
    if (this.textAttr != null)
      localJSONObject.put("textAttr", this.textAttr.toJSONObject());
    return localJSONObject;
  }
}